import os
import sys
import time
import logging

from src.common.prj_path import get_abs_path

LOG = logging.getLogger()


class MyMatter(object):
    def __init__(self, parse_fsm_object_list):
        self.fsm_svg = get_abs_path('output', 'fsm.svg')
        self.parse_fsm_object_list = parse_fsm_object_list
        self.app_notice = []
        self.order_dict = {}
        self._have_exception = False
        self._have_listend = None

    def have_order(self):
        print(sys._getframe().f_code.co_name)
        return bool(self.order_dict)

    def check_order_again(self):
        print(f"Hello!")

    def clear_notice(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('swipe_to_show_notification')
        func()
        xpath = "//android.widget.ImageButton[contains(@resource-id,'com.android.systemui:id/dismiss_notifications_button')]"
        func = self.get_parse_func('click_xpath_if_exist')
        func(xpath)

    def open_qn_mainActivity(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('start_activity')
        func('com.taobao.qianniu', '.desktop.ui.MainActivity')

    def goto_waitSendView(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('click_text')
        func('工作台')
        func('订单管理')
        func('订单管理')
        func = self.get_parse_func('wait_element')
        func('待发货')
        func = self.get_parse_func('click_xpath')
        xpath = "//android.view.View[contains(@text,'待发货')]"
        func(xpath)

    def parse_order(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('get_all_text')
        all_text = func()
        func = self.get_parse_func('get_order_dict')
        self.order_dict = func(all_text)

    def start_deliver(self):
        print(sys._getframe().f_code.co_name)

    def check_pop_ups(self):
        print(sys._getframe().f_code.co_name)

    def check_adb(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('check_phone_connected')
        try:
            func()
        except Exception as e:
            LOG.error(e)
            self._have_listend = False
        else:
            self._have_listend = True
        LOG.info('self._have_listend={}'.format(self._have_listend))

    def have_qn_msg(self):
        print(sys._getframe().f_code.co_name)
        return 'com.taobao.qianniu' in self.app_notice

    def have_exception(self):
        return bool(self._have_exception)

    def clear_exception_flag(self):
        self._have_exception = False

    def set_exception_flag(self):
        self._have_exception = True
        LOG.debug('set_exception_flag={}'.format(self._have_exception))

    def screenshot(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('screenshot')
        func()

    def have_listend(self):
        return self._have_listend

    def unlock_phone(self):
        print(sys._getframe().f_code.co_name)
        func = self.get_parse_func('unlock_phone')
        func()

    def get_app_notice(self):
        time.sleep(5)
        func = self.get_parse_func('get_all_notification_package')
        self.app_notice = func()
        print(self.app_notice)

    def get_parse_func(self, function_name):
        for parse_object in self.parse_fsm_object_list:
            if hasattr(parse_object, function_name):
                return getattr(parse_object, function_name)
        else:
            raise Exception('not find {} in all parse fsm object'.format(function_name))

    def show_graph(self):
        if os.name == 'nt':
            self.get_graph().draw(self.fsm_svg, prog='dot', format='svg')
            time.sleep(1)
